﻿using System;
using System.Collections;
using SteeringBehaviors;
using UnityEngine;

// Token: 0x020001BA RID: 442
public class AIShooterController : MonoBehaviour
{
	// Token: 0x060008DC RID: 2268 RVA: 0x00053C6C File Offset: 0x00051E6C
	private void Awake()
	{
		this.anim = base.GetComponent<Animator>();
		this.userSpine = this.anim.GetBoneTransform(HumanBodyBones.Spine);
		this.movement = base.GetComponent<MovementCharacter>();
		this.walk = this.movement.walkController;
		this.walk.Initialize(this.movement);
		this.aimAngleSinFloat = Animator.StringToHash("aimAngleSin");
		this.aimBool = Animator.StringToHash("aiming");
		this.gunTypeInt = Animator.StringToHash("gunType");
		this.ownerEntity = base.GetComponent<EntityBase>();
		this.accurateFire = GlobalPreferences.AIAccurateShooting.value;
		this.inaccuracyFactor = (float)GlobalPreferences.AIInaccuracyFactor.value;
		this.predictiveAiming = GlobalPreferences.AIPredictiveAiming.value;
		this.burstFire = GlobalPreferences.AIBurstFire.value;
		if (GlobalPreferences.AIRandomIntervals.value)
		{
			this.currentInterval += UnityEngine.Random.Range(0f, 1.5f);
		}
		this.EquipRaygun();
	}

	// Token: 0x060008DD RID: 2269 RVA: 0x00053D74 File Offset: 0x00051F74
	private void Update()
	{
		if (this.aiming)
		{
			if (this.aimTargetEntity == null)
			{
				this.StopAiming();
				return;
			}
			if (this.autoReposition)
			{
				this.UpdateDistancePositionState();
			}
			else
			{
				this.UpdateAimAlignmentState();
			}
		}
	}

	// Token: 0x060008DE RID: 2270 RVA: 0x00053DC0 File Offset: 0x00051FC0
	private void LateUpdate()
	{
		if (this.aiming)
		{
			if (this.aimTargetEntity != null && !this.pauseAiming)
			{
				this.UpdateAim();
				if (this.autoReposition)
				{
					this.UpdatePositioning();
				}
			}
			if (this.isFiring && !this.pauseAiming)
			{
				this.UpdateFiring();
			}
		}
	}

	// Token: 0x060008DF RID: 2271 RVA: 0x00053E27 File Offset: 0x00052027
	private void OnDestroy()
	{
		this.CleanupCurrentGun();
	}

	// Token: 0x060008E0 RID: 2272 RVA: 0x00053E30 File Offset: 0x00052030
	private Vector3 GetHandCenterPos()
	{
		Vector3 result;
		if (this.anim.GetBoneTransform(HumanBodyBones.RightMiddleIntermediate))
		{
			Vector3 a = (this.anim.GetBoneTransform(HumanBodyBones.RightMiddleProximal).position + this.anim.GetBoneTransform(HumanBodyBones.RightHand).position) * 0.5f;
			Vector3 b = (this.anim.GetBoneTransform(HumanBodyBones.RightLittleProximal).position + this.anim.GetBoneTransform(HumanBodyBones.RightThumbProximal).position) * 0.5f;
			result = (a + b) * 0.5f;
		}
		else
		{
			result = this.anim.GetBoneTransform(HumanBodyBones.RightHand).position;
		}
		return result;
	}

	// Token: 0x060008E1 RID: 2273 RVA: 0x00053EE8 File Offset: 0x000520E8
	public void EquipRaygun()
	{
		if (this.gunObj != null)
		{
			this.CleanupCurrentGun();
		}
		this.gunObj = UnityEngine.Object.Instantiate<GameObject>((GameObject)Resources.Load("Raygun/raygunLitePrefab"));
		this.gun = this.gunObj.GetComponent<AIRaygun>();
		this.EquipGun(this.gun, this.gunObj);
	}

	// Token: 0x060008E2 RID: 2274 RVA: 0x00053F4C File Offset: 0x0005214C
	public void EquipSMG()
	{
		if (this.gunObj != null)
		{
			this.CleanupCurrentGun();
		}
		this.gunObj = UnityEngine.Object.Instantiate<GameObject>((GameObject)Resources.Load("SMG/MP-40Prefab"));
		this.gun = this.gunObj.GetComponent<AISMG>();
		this.EquipGun(this.gun, this.gunObj);
	}

	// Token: 0x060008E3 RID: 2275 RVA: 0x00053FB0 File Offset: 0x000521B0
	private void EquipGun(AIGun gun, GameObject gunObj)
	{
		gun.SetOwner(base.gameObject);
		this.anim.SetInteger(this.gunTypeInt, gun.AnimGunType);
		Vector3 forward = this.anim.GetBoneTransform(HumanBodyBones.RightMiddleProximal).position - this.anim.GetBoneTransform(HumanBodyBones.RightHand).position;
		Vector3 upwards = this.anim.GetBoneTransform(HumanBodyBones.RightIndexProximal).position - this.anim.GetBoneTransform(HumanBodyBones.RightLittleProximal).position;
		Quaternion rotation = Quaternion.LookRotation(forward, upwards);
		gunObj.transform.SetParent(this.anim.GetBoneTransform(HumanBodyBones.RightHand));
		gunObj.transform.localPosition = this.anim.GetBoneTransform(HumanBodyBones.RightHand).InverseTransformPoint(this.GetHandCenterPos());
		gunObj.transform.rotation = rotation;
	}

	// Token: 0x060008E4 RID: 2276 RVA: 0x00054083 File Offset: 0x00052283
	public void UnequipGun()
	{
		if (this.aiming)
		{
			this.StopAiming();
		}
		this.CleanupCurrentGun();
		UnityEngine.Object.Destroy(this);
	}

	// Token: 0x060008E5 RID: 2277 RVA: 0x000540A2 File Offset: 0x000522A2
	private void CleanupCurrentGun()
	{
		if (this.gunObj == null)
		{
			return;
		}
		if (this.isFiring)
		{
			this.StopFiring();
		}
		UnityEngine.Object.Destroy(this.gunObj);
	}

	// Token: 0x060008E6 RID: 2278 RVA: 0x000540D4 File Offset: 0x000522D4
	public void StartFiring(bool autoSeek)
	{
		if (!this.aimTargetEntity || !this.aimTargetEntity.GetAnimator().GetBoneTransform(HumanBodyBones.Spine))
		{
			if (GlobalPreferences.ScriptAuxLogging.value)
			{
				Debug.Log("No target to start firing at. Please set a traget first.");
			}
			return;
		}
		this.isFiring = true;
		this.autoReposition = autoSeek;
		if (!this.aiming)
		{
			this.StartAim();
		}
	}

	// Token: 0x060008E7 RID: 2279 RVA: 0x00054145 File Offset: 0x00052345
	public void StartSeekFiring(EntityBase target)
	{
		if (target != null)
		{
			this.SetAimTarget(target);
		}
		this.StartFiring(true);
	}

	// Token: 0x060008E8 RID: 2280 RVA: 0x00054161 File Offset: 0x00052361
	public void StopFiring()
	{
		this.isFiring = false;
	}

	// Token: 0x060008E9 RID: 2281 RVA: 0x0005416A File Offset: 0x0005236A
	public void StopSeekFiring()
	{
		this.autoReposition = false;
		this.StopAiming();
	}

	// Token: 0x060008EA RID: 2282 RVA: 0x0005417C File Offset: 0x0005237C
	public void Fire()
	{
		if (this.aiming)
		{
			this.gun.Fire((!this.accurateFire) ? this.inaccuracyFactor : 0f);
			EventManager.SendEvent(new AIWeaponFireEvent(this.ownerEntity));
		}
	}

	// Token: 0x060008EB RID: 2283 RVA: 0x000541CC File Offset: 0x000523CC
	public void SetAimTarget(EntityBase target)
	{
		if (target == null)
		{
			target = null;
			if (this.aiming)
			{
				this.StopAiming();
			}
			return;
		}
		if (!target.isGiantess && !target.isMicro)
		{
			return;
		}
		Humanoid humanoid = target as Humanoid;
		if (humanoid)
		{
			this.aimTargetEntity = humanoid;
			if (humanoid.GetAnimator().GetBoneTransform(HumanBodyBones.Spine))
			{
				this.aimTarget = this.aimTargetEntity.GetAnimator().GetBoneTransform(HumanBodyBones.Spine);
			}
			else
			{
				this.aimTarget = this.aimTargetEntity.transform;
			}
			this.StartAim();
		}
	}

	// Token: 0x060008EC RID: 2284 RVA: 0x00054273 File Offset: 0x00052473
	private void StartAim()
	{
		this.anim.SetBool(this.aimBool, true);
		this.aiming = true;
		this.pauseAiming = false;
		this.aimDirState = AIShooterController.AimAlignmentState.WholeBody;
		this.distanceState = AIShooterController.DistancePositioningState.Okay;
		base.StartCoroutine(this.OrientGun());
	}

	// Token: 0x060008ED RID: 2285 RVA: 0x000542B0 File Offset: 0x000524B0
	private IEnumerator OrientGun()
	{
		bool transitionStarted = false;
		while (!transitionStarted || !this.anim.GetAnimatorTransitionInfo(this.gun.AnimLayer).IsName(this.gun.AnimTransitionName))
		{
			if (!transitionStarted && this.anim.GetAnimatorTransitionInfo(this.gun.AnimLayer).IsName(this.gun.AnimTransitionName))
			{
				transitionStarted = true;
			}
			yield return null;
		}
		yield return new WaitForSeconds(2f);
		this.gun.PointAt(this.aimTarget);
		yield return new WaitForSeconds(6f);
		Vector3 gunToTarget = this.aimTarget.position - this.gunObj.transform.position;
		if (Vector3.Angle(gunToTarget, this.gunObj.transform.forward) > 10f)
		{
			this.gun.PointAt(this.aimTarget);
		}
		yield break;
	}

	// Token: 0x060008EE RID: 2286 RVA: 0x000542CB File Offset: 0x000524CB
	public void ReorientGun()
	{
		this.gun.PointAt(this.aimTarget);
	}

	// Token: 0x060008EF RID: 2287 RVA: 0x000542E0 File Offset: 0x000524E0
	private void UpdateAimAlignmentState()
	{
		if (!this.ownerEntity.movement.move)
		{
			this.aimDirState = AIShooterController.AimAlignmentState.WholeBody;
		}
		else
		{
			Vector3 to = this.aimTargetEntity.transform.position - base.transform.position;
			to.y = 0f;
			Vector3 forward = base.transform.forward;
			forward.y = 0f;
			float f = Vector3.Angle(forward, to);
			if (Mathf.Abs(f) < 70f)
			{
				this.aimDirState = AIShooterController.AimAlignmentState.UpperBody;
			}
			else
			{
				this.aimDirState = AIShooterController.AimAlignmentState.None;
			}
		}
		if (this.aimDirState != AIShooterController.AimAlignmentState.None && this.pauseAiming)
		{
			this.pauseAiming = false;
		}
	}

	// Token: 0x060008F0 RID: 2288 RVA: 0x0005439C File Offset: 0x0005259C
	private void UpdateDistancePositionState()
	{
		if (this.aimAngleSin > 0.9f)
		{
			this.distanceState = AIShooterController.DistancePositioningState.TooClose;
			return;
		}
		AIShooterController.DistancePositioningState distancePositioningState = this.distanceState;
		float num = Vector3.Magnitude(base.transform.position - this.aimTargetEntity.transform.position);
		float heightMesh = this.aimTargetEntity.HeightMesh;
		if (this.distanceState == AIShooterController.DistancePositioningState.SlightlyTooFar && num < heightMesh * 2.9f)
		{
			this.distanceState = AIShooterController.DistancePositioningState.Okay;
		}
		if (num < heightMesh * 3f)
		{
			this.distanceState = AIShooterController.DistancePositioningState.Okay;
		}
		else if (this.distanceState == AIShooterController.DistancePositioningState.TooFar && num < heightMesh * 5.9f)
		{
			this.distanceState = AIShooterController.DistancePositioningState.SlightlyTooFar;
		}
		else if (num < heightMesh * 6f)
		{
			this.distanceState = AIShooterController.DistancePositioningState.SlightlyTooFar;
		}
		else
		{
			this.distanceState = AIShooterController.DistancePositioningState.TooFar;
		}
		if (distancePositioningState != this.distanceState)
		{
			this.anim.Play("Idle");
			this.movement.Stop();
			this.repositioning = false;
		}
	}

	// Token: 0x060008F1 RID: 2289 RVA: 0x000544A8 File Offset: 0x000526A8
	private void UpdateAim()
	{
		if (this.aimDirState == AIShooterController.AimAlignmentState.None)
		{
			if (this.anim.GetBool(this.aimBool))
			{
				this.anim.SetBool(this.aimBool, false);
				this.pauseAiming = true;
			}
			return;
		}
		if (!this.anim.GetBool(this.aimBool))
		{
			this.anim.SetBool(this.aimBool, true);
		}
		this.UpdateArmAim();
		this.UpdateAimAlignment();
	}

	// Token: 0x060008F2 RID: 2290 RVA: 0x00054525 File Offset: 0x00052725
	public void StopAiming()
	{
		this.anim.SetBool(this.aimBool, false);
		this.aiming = false;
		this.aimTargetEntity = null;
		if (this.isFiring)
		{
			this.StopFiring();
		}
	}

	// Token: 0x060008F3 RID: 2291 RVA: 0x00054558 File Offset: 0x00052758
	private void UpdateArmAim()
	{
		float num = this.GetTargetPos().y - this.userSpine.position.y;
		float num2 = Vector3.Distance(this.userSpine.position, this.GetTargetEntityPos());
		this.aimAngleSin = num / num2;
		if (this.distanceState == AIShooterController.DistancePositioningState.TooFar)
		{
			this.aimAngleSin += 0.3f;
		}
		this.anim.SetFloat(this.aimAngleSinFloat, this.aimAngleSin);
	}

	// Token: 0x060008F4 RID: 2292 RVA: 0x000545E0 File Offset: 0x000527E0
	private void UpdateAimAlignment()
	{
		if (this.aimDirState == AIShooterController.AimAlignmentState.WholeBody)
		{
			base.transform.LookAt(new Vector3(this.GetTargetPos().x, base.transform.position.y, this.GetTargetPos().z));
		}
		else
		{
			Vector3 toDirection = this.GetTargetEntityPos() - this.anim.GetBoneTransform(HumanBodyBones.Spine).position;
			toDirection.y = 0f;
			this.anim.GetBoneTransform(HumanBodyBones.Spine).rotation = Quaternion.FromToRotation(base.transform.forward, toDirection) * this.anim.GetBoneTransform(HumanBodyBones.Spine).rotation;
		}
	}

	// Token: 0x060008F5 RID: 2293 RVA: 0x000546A0 File Offset: 0x000528A0
	private void UpdateFiring()
	{
		this.currentInterval -= Time.deltaTime;
		if (this.currentInterval <= 0f)
		{
			if (!this.burstFire)
			{
				this.Fire();
				this.currentInterval = this.firingInterval;
				if (GlobalPreferences.AIRandomIntervals.value)
				{
					this.currentInterval += UnityEngine.Random.Range(0f, 1.5f);
				}
			}
			else
			{
				this.currentBurstFireInterval -= Time.deltaTime;
				if (this.currentBurstFireInterval <= 0f)
				{
					this.Fire();
					this.currentBurstRound--;
					if (this.currentBurstRound != 0)
					{
						this.currentBurstFireInterval = this.burstFireInterval;
					}
					else
					{
						this.currentBurstRound = this.burstFireRounds;
						this.currentInterval = this.firingInterval;
						if (GlobalPreferences.AIRandomIntervals.value)
						{
							this.currentInterval += UnityEngine.Random.Range(0f, 1.5f);
						}
					}
				}
			}
		}
	}

	// Token: 0x060008F6 RID: 2294 RVA: 0x000547B4 File Offset: 0x000529B4
	private void UpdatePositioning()
	{
		if (this.distanceState == AIShooterController.DistancePositioningState.TooClose)
		{
			if (!this.repositioning)
			{
				this.anim.Play("Walk Backwards");
				this.movement.StartMoveInDirectionBehavior(new VectorKinematic(base.transform.position - this.aimTargetEntity.transform.position));
				this.repositioning = true;
			}
		}
		else if (this.distanceState != AIShooterController.DistancePositioningState.Okay)
		{
			if (this.distanceState == AIShooterController.DistancePositioningState.SlightlyTooFar)
			{
				if (!this.repositioning)
				{
					this.anim.Play("Walk");
					this.movement.StartMoveInDirectionBehavior(new VectorKinematic(this.aimTargetEntity.transform.position - base.transform.position));
					this.repositioning = true;
				}
			}
			else if (!this.repositioning)
			{
				this.anim.Play("Run");
				this.movement.StartSeekBehavior(new TransformKinematic(this.aimTargetEntity.transform), 0f, 0f);
				this.repositioning = true;
			}
		}
	}

	// Token: 0x060008F7 RID: 2295 RVA: 0x000548E0 File Offset: 0x00052AE0
	private Vector3 GetTargetPos()
	{
		if (!this.predictiveAiming || !this.aimTargetEntity.movement.move)
		{
			return this.aimTarget.position;
		}
		return this.aimTarget.position + this.aimTargetEntity.movement.velocity * 0.5f;
	}

	// Token: 0x060008F8 RID: 2296 RVA: 0x00054944 File Offset: 0x00052B44
	private Vector3 GetTargetEntityPos()
	{
		if (!this.predictiveAiming || !this.aimTargetEntity.movement.move)
		{
			return this.aimTargetEntity.transform.position;
		}
		return this.aimTargetEntity.transform.position + this.aimTargetEntity.movement.velocity * 0.5f;
	}

	// Token: 0x060008F9 RID: 2297 RVA: 0x000549B1 File Offset: 0x00052BB1
	public void SetBurstFireMode(bool enable)
	{
		this.burstFire = enable;
		if (enable)
		{
			this.currentBurstFireInterval = 0f;
			this.currentBurstRound = this.burstFireRounds;
		}
	}

	// Token: 0x060008FA RID: 2298 RVA: 0x000549D7 File Offset: 0x00052BD7
	public void SetProjectileColor(int r, int g, int b)
	{
		this.gun.SetColor(r, g, b);
	}

	// Token: 0x060008FB RID: 2299 RVA: 0x000549E7 File Offset: 0x00052BE7
	public void SetProjectileSpeed(float speedMult)
	{
		this.gun.SetProjectileSpeed(speedMult);
	}

	// Token: 0x060008FC RID: 2300 RVA: 0x000549F5 File Offset: 0x00052BF5
	public void SetProjectileScale(float scaleMult)
	{
		this.gun.SetProjectileScale(scaleMult);
	}

	// Token: 0x060008FD RID: 2301 RVA: 0x00054A03 File Offset: 0x00052C03
	public void SetRaygunFiringSound(string clip)
	{
		this.gun.SetupFiringSound(clip);
	}

	// Token: 0x060008FE RID: 2302 RVA: 0x00054A11 File Offset: 0x00052C11
	public void SetProjectileImpactSound(string clip)
	{
		this.gun.SetupProjectileImpactSound(clip);
	}

	// Token: 0x04000EA7 RID: 3751
	private EntityBase ownerEntity;

	// Token: 0x04000EA8 RID: 3752
	private Transform userSpine;

	// Token: 0x04000EA9 RID: 3753
	private Animator anim;

	// Token: 0x04000EAA RID: 3754
	private MovementCharacter movement;

	// Token: 0x04000EAB RID: 3755
	private WalkController walk;

	// Token: 0x04000EAC RID: 3756
	private GameObject gunObj;

	// Token: 0x04000EAD RID: 3757
	private AIGun gun;

	// Token: 0x04000EAE RID: 3758
	public bool aiming;

	// Token: 0x04000EAF RID: 3759
	private bool pauseAiming;

	// Token: 0x04000EB0 RID: 3760
	private float aimAngleSin;

	// Token: 0x04000EB1 RID: 3761
	private bool autoReposition;

	// Token: 0x04000EB2 RID: 3762
	private bool repositioning;

	// Token: 0x04000EB3 RID: 3763
	public bool isFiring;

	// Token: 0x04000EB4 RID: 3764
	public bool burstFire;

	// Token: 0x04000EB5 RID: 3765
	public int burstFireRounds = 3;

	// Token: 0x04000EB6 RID: 3766
	private int currentBurstRound = 3;

	// Token: 0x04000EB7 RID: 3767
	public float burstFireInterval = 0.75f;

	// Token: 0x04000EB8 RID: 3768
	private float currentBurstFireInterval;

	// Token: 0x04000EB9 RID: 3769
	public float firingInterval = 4f;

	// Token: 0x04000EBA RID: 3770
	private float currentInterval = 3f;

	// Token: 0x04000EBB RID: 3771
	public bool accurateFire;

	// Token: 0x04000EBC RID: 3772
	public float inaccuracyFactor;

	// Token: 0x04000EBD RID: 3773
	public bool predictiveAiming;

	// Token: 0x04000EBE RID: 3774
	private Humanoid aimTargetEntity;

	// Token: 0x04000EBF RID: 3775
	private Transform aimTarget;

	// Token: 0x04000EC0 RID: 3776
	private int aimAngleSinFloat;

	// Token: 0x04000EC1 RID: 3777
	private int aimBool;

	// Token: 0x04000EC2 RID: 3778
	private int gunTypeInt;

	// Token: 0x04000EC3 RID: 3779
	private AIShooterController.DistancePositioningState distanceState;

	// Token: 0x04000EC4 RID: 3780
	private AIShooterController.AimAlignmentState aimDirState;

	// Token: 0x04000EC5 RID: 3781
	private const int gunAdjustmentCheckDelay = 2;

	// Token: 0x04000EC6 RID: 3782
	private const int gunReadjustmentCheckDelay = 6;

	// Token: 0x04000EC7 RID: 3783
	private const int gunReadjustmentAngleThreshold = 10;

	// Token: 0x020001BB RID: 443
	private enum DistancePositioningState
	{
		// Token: 0x04000EC9 RID: 3785
		TooClose,
		// Token: 0x04000ECA RID: 3786
		Okay,
		// Token: 0x04000ECB RID: 3787
		SlightlyTooFar,
		// Token: 0x04000ECC RID: 3788
		TooFar
	}

	// Token: 0x020001BC RID: 444
	private enum AimAlignmentState
	{
		// Token: 0x04000ECE RID: 3790
		WholeBody,
		// Token: 0x04000ECF RID: 3791
		UpperBody,
		// Token: 0x04000ED0 RID: 3792
		None
	}
}
